import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Project {
    public static void main(String[] args){
        MyList myList = new MyList();
        MyBox myBox = new MyBox();
    }
}

/*
JList
列表
    1.使用数组初始化列表
    2.实现接口ListSelectionListener的
    public void valueChanged(ListSelectionEvent e) {
    }

    创建两个列表，一个单选，一个多选

 */
class MyList extends JFrame implements ListSelectionListener {
    JList list1,list2;
    JLabel lb1,lb2;
    String[] fruit = {"苹果","香蕉","西瓜","猕猴桃"};
    Color color[] = {Color.CYAN,Color.BLUE,Color.green,Color.MAGENTA,Color.RED};

    MyList(){
        list1 = new JList(fruit);
        list2 = new JList(color);
        lb1 = new JLabel();
        lb2 = new JLabel();

        //初始化列表配置
        list1.setVisibleRowCount(2);
        JScrollPane jsp = new JScrollPane(list1);
        list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        Container con =  getContentPane();
        con.setBackground(Color.CYAN);
        con.setLayout(new GridLayout(1,4,10,10));
        con.add(jsp);

        con.add(lb1);
        con.add(list2);
        con.add(lb2);
        setSize(500,400);
        setVisible(true);

        list1.addListSelectionListener(this);
        list2.addListSelectionListener(this);
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if(e.getSource() == list1){//获取是哪个列表触发的
            if(e.getValueIsAdjusting()) {//获取是哪项被选中
                System.out.println(list1.getSelectedValue());
                lb1.setText(list1.getSelectedValue().toString());//显示选中标签
            }

        }else if(e.getSource() == list2){
            if(e.getValueIsAdjusting()) {
                //System.out.println(list2.getSelectedValue());
                //System.out.println(list2.getSelectedIndex());
                getContentPane().setBackground(color[list2.getSelectedIndex()]);//改变背景色
            }
        }
    }
}

/*
JComboBox
组合框
    1.使用数组初始化组合框
    2.方法
        getSelectedIndex
            获取当前选中的索引
        removeItemAt
            删除索引对应的项
        setSelectedIndex
            设置当前项到指定索引
 */
class MyBox extends JFrame implements ItemListener, ActionListener{
    String[] fruit = {"苹果","香蕉","西瓜","猕猴桃",};
    JTextField jtf;
    JComboBox box;
    MyBox(){
        jtf = new JTextField();
        box = new JComboBox(fruit);
        setSize(600,600);
        Container con = getContentPane();
        con.setLayout(new GridLayout());
        this.add(box);
        this.add(jtf);
        setVisible(true);
        jtf.addActionListener(this::actionPerformed);
        box.addItemListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == jtf){

            int index = box.getSelectedIndex();
            String str = jtf.getText();

            box.removeItemAt(index);
            box.insertItemAt(str,index);
            box.setSelectedIndex(index);
            //box.addItem(str);
        }else {

        }
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED){
            String str = e.getItem().toString();
            jtf.setText(str);
        }

    }
}